<!DOCTYPE html>
<html>
<head>
  <title>Energia Reference - Tutorials </title>
  <link rel="shortcut icon" type="image/x-icon" href="http://energia.nu/img/eicon.png">
  <link rel='stylesheet' href='energiaWideRender.css' 
type='text/css' />
    <!--HeaderText--><style type='text/css'></style>  
  <meta name='robots' content='index,follow' />
  <meta name="Author" content="Robert Wessels" />
  <meta name="Publisher" content="Energia" />
  <meta name="Keywords" content="Energia, Wiring, Processing, Maker, LaunchPad, Arduino, Texas Instruments, Sketch, MSP430, mspgcc, mpsdebug, Electronic Arts, Programming, C, C++, Robert Wessels" />
  <meta name="Description" content="Energia is a rapid prototyping platform for the Texas Instruments MCU Launchpad. Energia is based on Wiring and Arduino and uses the Processing IDE." />
  <meta name="Copyright" content="All contents copyright Robert Wessels" />

</head>
<body>
<div id="page">
  <!--PageHeaderFmt-->
  <div id="pageheader">
    <div class="title"><a href='http://energia.nu'>Energia</a></div>
  </div>
  <!--/PageHeaderFmt-->

  <!--PageLeftFmt-->
  <div id="pagenav">
    <div id="navbar">
  	<p><a class='wikilink' href='http://energia.nu/'>Home</a>
<a class='wikilink' href='http://energia.nu/download'>Download</a>
<a class='wikilink' href='Guide_index.html'>Getting Started</a>
<a class='wikilink' href='index.html'>Reference</a>
<a class="wikilink" href="http://energia.nu/help.html">Getting Help</a>
<a class="nav" href="http://energia.nu/faq.html">FAQ</a>
<a class="wikilink" href="http://energia.nu/projects.html">Projects Using Energia</a>
<a class="nav" href="http://energia.nu/contact.html">Contact Us</a>
</p>


<p class='vspace'></p>

    </div>
  </div>
  <!--/PageLeftFmt-->

  <div id="pagetext">
  <!--PageText-->
<div id='wikitext'>
<p><em>Examples &gt; Control Structures</em>
</p>
<p class='vspace'></p><h3>For Loop (aka The Knight Rider)</h3>
<p>A for loop is a section of code that repeat until a conditional is met.  Usually this is based on a counter that you define in a code.  For example, run this code as long as the counter is under 100 and each time through the loop increment the counter by one.  If we intialize the counter to zero in the beginning, then the loop will run through 100 iterations of the code before exiting the loop and continuing on with the rest of program.
</p>
<p class='vspace'></p><h3>Hardware Required</h3>
<ul><li>MSP430 LaunchPad
</li><li>(9) 220 ohm resistors
</li><li>(9) <span class='wikiword'>LEDs</span>
</li><li>hook-up wire
</li><li>breadboard
</li></ul>
<h3>Relevant Groundwork</h3>
<p>Often you want to iterate over a series of pins and do something to each one.  For instance, this example blinks 9 <span class='wikiword'>LEDs</span>  attached the LaunchPad by using a  <a class='wikilink' href='For.html'><strong>for()</strong></a> loop to cycle back and forth through digital pins 2-10. The LEDS are turned on and off, in sequence, by using both the <a class='wikilink' href='DigitalWrite.html'>digitalWrite()</a>  and <a class='wikilink' href='Delay.html'>delay()</a> functions . 
</p>
<p class='vspace'></p><p>We also call this example "<a class='urllink' href='http://en.wikipedia.org/wiki/KITT' rel='nofollow'>Knight Rider</a>" in memory of a TV-series from the 80's where David Hasselhoff had an AI machine named KITT driving his Pontiac. The car had been augmented with plenty of <span class='wikiword'>LEDs</span> in all possible sizes performing flashy effects. In particular, it had a display that scanned back and forth across a line. This example duplicates the KITT display.
</p>
<p class='vspace'></p><h3>Circuit</h3>
<p>Connect nice LEDS, with 220 ohm resistors in series, to digital pins 2-10 on your LaunchPad.
</p>
<p class='vspace'></p><div><img height='400px' src='../reference/img/ForLoop_bb.png' alt='' title='' /></div>
<p class='vspace'></p><p><span style='font-size:83%'>image developed using <a class='urllink' href='http://www.fritzing.org' rel='nofollow'>Fritzing</a>. For more circuit examples, see the <a class='urllink' href='http://fritzing.org/projects/' rel='nofollow'>Fritzing project page</a> </span>
</p>
<p class='vspace'></p><p><h3>Schematic:</h3>
</p>
<p class='vspace'></p><div><img width='400px' src='../reference/img/ForLoop_schem.png' alt='' title='' /></div>
<p class='vspace'></p><h3>Code Explanation</h3>
<p>The code below begins by utilizing a <code>for()</code> loop to assign digital pins 2-10 as outputs for the 9 <span class='wikiword'>LEDs</span> used. 
</p>
<p class='vspace'></p><p>In the main loop of the code, two <code>for()</code> loops are used to loop incrementally, stepping through the <span class='wikiword'>LEDs</span>, one by one, from pin 2 to pin 10. Once pin 10 is lit, the process reverses, stepping back down through each LED. 
</p>
<p class='vspace'></p><h3>Code</h3>
<pre>
/*
  For Loop Iteration
 
  Demonstrates the use of a for() loop. 
  Lights multiple LEDs in sequence, then in reverse.
 
  The circuit:
  * LEDs from pins 2 through 10 to ground
 
  created 2006
  by David A. Mellis
  modified 16 Apr 2011
  by Adrian Fernandez 

  This example code is in the public domain.
 
*/

int timer = 100;           // The higher the number, the slower the timing.

void setup() {
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 2; thisPin < 11; thisPin++)  {
    pinMode(thisPin, OUTPUT);      
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 2; thisPin < 11; thisPin++) { 
    // turn the pin on:
    digitalWrite(thisPin, HIGH);   
    delay(timer);                  
    // turn the pin off:
    digitalWrite(thisPin, LOW);    
  }

  // loop from the highest pin to the lowest:
  for (int thisPin = 10; thisPin >= 2; thisPin--) { 
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }
}
</pre>
<p class='vspace'></p><h3>See Also:</h3>
<ul><li><a class='wikilink' href='For.html'>for()</a>
</li><li><a class='wikilink' href='DigitalWrite.html'>digitalWrite()</a>
</li><li><a class='wikilink' href='Delay.html'>delay()</a>
</li><li><a class='wikilink' href='Tutorial_WhileLoop.html'>WhileLoop</a> - Use a While Loop to calibrate a sensor while a button is being pressed.
</li><li><a class='wikilink' href='Tutorial_SwitchCase.html'>SwitchCase</a> - Choose between a number of discrete values in a manner that is the equivalent of using multiples If  statements. This example shows how to divide a sensor's range into a set of four bands and to take four different actions depending on which band the result is in.
</li><li><a class='wikilink' href='Tutorial_Array.html'>Array</a>: a variation on the For Loop example that demonstrates how to use an array.
</li></ul>
<p class='vspace'></p><p><a class='wikilink' href='Guide_index.html'>Getting Started Home</a>
</p>
<p class='vspace'></p><p><em>Corrections, suggestions, and new documentation should be posted to the <a class='urllink' href='http://forum.43oh.com/forum/28-energia/' rel='nofollow'>Forum</a>.</em>
</p>
<p class='vspace'></p><p>The text of the Energia reference is licensed under a
<a class='urllink' href='http://creativecommons.org/licenses/by-sa/3.0/' rel='nofollow'>Creative Commons Attribution-ShareAlike 3.0 License</a>.  Energia reference is based on the Arduino reference. Code samples in the reference are released into the public domain.
</p>
</div>


  </div>

  <!--PageFooterFmt-->
  <div id="pagefooter">
&copy;Energia | 
  <a href='#'>Edit Page</a> | <a href='#'>Page History</a> | <a href=#' target='_blank'>Printable View</a> | <a href='#'>All Recent Site Changes</a>
  </div>
  <!--/PageFooterFmt-->

</div>
</body>
</html>
